package com.fresh.bus.chat.ctrl;

import java.net.URLDecoder;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.data.redis.support.collections.RedisSet;
import org.springframework.stereotype.Controller;

import com.fresh.bus.canoe.command.annotation.CanoeCommand;
import com.fresh.bus.canoe.message.Request;
import com.fresh.bus.canoe.message.Response;
import com.fresh.bus.canoe.session.Role;
import com.fresh.bus.canoe.session.Session;
import com.fresh.bus.chat.util.MessageUtils;

@Controller("selectCtrl")
public class SelectCtrl extends BaseCtrl {

	@CanoeCommand(value = OpCodes.SEND_CANDIDATAE_WORD)
	public Response sendWords(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		String id = session.getRole().getId();
		Response response = new Response(OpCodes.SEND_CANDIDATAE_WORD);
		String to = (String) data.get("to");

		try {
			List<Map<Object,Object>> l = this.scheduleManager.askForCandidate(id,
					data);
			response.setData(l.toArray());
			
			//通知其他候选者
			Map<String,String> mine = this.userService.findUser(id);
			Map<String,String> report = this.userService.findReport(id);
			
			RedisSet<String> fans = this.scheduleManager.fans(to);
			fans.add(to);
			for (String f : fans) {
				Session s2 = roleManager.getSession(f);
				Response r2 = new Response(OpCodes.SHOW_CANDIDATAE_WORD);
				Map<Object,Object> map = new HashMap<Object,Object>();
				map.put("user", mine);
				map.put("report", report);
				r2.setData(map);
				s2.send(r2);
			}
			
		} catch (Exception e) {
			log.error("Login failed...", e);
			response.setData(e.getMessage());
			return response;
		}
		return response;
	}

	@CanoeCommand(value = OpCodes.SEL_TALKER)
	public Response selectTalk(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		String to = (String) data.get("user");
		String rid = (String) session.getAttribute("rid");
		String id = session.getRole().getId();
		Response response = new Response(OpCodes.SEL_TALKER);
		try {
			Integer flower = (Integer) data.get("flower");
			if (flower > 0) {
				boolean flag = wealthService.giveFlower(to,id,flower);
				if(!flag) throw new Exception("玫瑰数量不足");
			}
			this.scheduleManager.setTalker(to, id);
			talkService.startTalk(id,to);
			response.setData(true);
		} catch (Exception e) {
			log.error("Login failed...", e);
			response.setData(e.getMessage());
		}
		return response;
	}

	@CanoeCommand(value = OpCodes.CANCEL_SEL_TALKER)
	public Response cancelSelect(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		String id = session.getRole().getId();
		String rid = (String) session.getAttribute("rid");
		Response response = new Response(OpCodes.CANCEL_SEL_TALKER);
		try{
			scheduleManager.cancelElection(id,rid);
			response.setData(true);
		}catch (Exception e) {
			log.error("Login failed...", e);
			response.setData(MessageUtils.getErrorMessage());
		}
		return response;
	}

	@CanoeCommand(value = OpCodes.BEGIN_SEL_TALKER)
	public Response beginSelect(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		String id = session.getRole().getId();
		String question = (String) data.get("question");
		Response response = new Response(OpCodes.BEGIN_SEL_TALKER);
		try {
			this.scheduleManager.notifyCandidate(session, question);
			response.setData(true);
		} catch (Exception e) {
			log.error("Login failed...", e);
			response.setData(MessageUtils.getErrorMessage());
		}
		return response;
	}
	

	@CanoeCommand(value = OpCodes.READ_LATEST_USERS)
	public Response latestUsers(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		try {
			int index = (Integer) data.get("i");
			String id = session.getRole().getId();
			Map<String,String> user = this.userService.findUser(id);
			//String province = this.userService.getProvince(user);
			String sex = user.get("sex").equals("female")?"male":"female";
			
			String province = (String) data.get("x");
			//province = URLDecoder.decode(province,"UTF-8");
			Response response = new Response(OpCodes.READ_LATEST_USERS);
			List<Map<String, String>> list = this.selectService
					.getLatestUsers(index,province,sex);
			if (list.isEmpty()) {
				response.setData(false);
			} else {
				response.setData(list.toArray());
			}
			return response;
		} catch (Exception e) {
			log.error("Query comment failed...", e);
			return null;
		}
	}	
}
